package com.example.demo.common;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;

import static com.example.demo.common.CodeMessageEnum.SUCCESS;

/**
 * @author JiJunpeng
 * @date 2018/7/25
 */
@Data
@NoArgsConstructor
public class Response {
    private String code;
    private String message;
    private Object data;

    public Response(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public Response(CodeMessageEnum codeMessage) {
        this.code = codeMessage.getCode();
        this.message = codeMessage.getMessage();
    }

    public Response replaceData(Object data) {
        this.data = data;
        return this;
    }

    public Response appendData(String key, Object data) {
        final HashMap<String, Object> map = new HashMap<>();
        map.put(key, data);
        this.data = map;
        return this;
    }

    public static Response success() {
        final Response response = new Response();
        response.code = SUCCESS.getCode();
        response.message = SUCCESS.getMessage();
        return response;
    }
}
